package com.ABdolphin.forum.services.impl;

import com.ABdolphin.forum.common.AppResult;
import com.ABdolphin.forum.common.ResultCode;
import com.ABdolphin.forum.dao.ArticleMapper;
import com.ABdolphin.forum.exception.ApplicationException;
import com.ABdolphin.forum.model.Article;
import com.ABdolphin.forum.model.Board;
import com.ABdolphin.forum.model.User;
import com.ABdolphin.forum.services.IArticleService;
import com.ABdolphin.forum.services.IBoardService;
import com.ABdolphin.forum.services.IUserService;
import com.ABdolphin.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ArticleServiceImpl implements IArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource(type = BoardServiceImpl.class)
    private IBoardService boardService;
    @Resource(type = UserServiceImpl.class)
    private IUserService userService;


    @Override
    public List<Article> selectAll() {
        List<Article> articles = articleMapper.selectAll();
        return articles;
    }

    @Override
    public List<Article> selectByBoardId(Long boardId) {
        if (boardId == null || boardId <= 0) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardService.selectById(boardId);
        if (board == null || board.getDeleteState() == 1) {
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        if (board.getState() == 1) {
            log.info(ResultCode.FAILED_BOARD_BANNED.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_BANNED));
        }
        return articleMapper.selectByBoardId(boardId);
    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        if (userId == null || userId <= 0) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user = userService.selectById(userId);
        if (user == null || user.getDeleteState() == 1) {
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        return articleMapper.selectByUserId(userId);
    }

    @Override
    public void create(Article article) {
        if (article == null || article.getBoardId() == null ||
                article.getUserId() == null ||
                StringUtil.isEmpty(article.getTitle()) ||
                StringUtil.isEmpty(article.getContent())) {
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article insertArticle = new Article();
        insertArticle.setBoardId(article.getBoardId());
        insertArticle.setUserId(article.getUserId());
        insertArticle.setTitle(article.getTitle());
        insertArticle.setContent(article.getContent());
        insertArticle.setVisitCount(0);
        insertArticle.setReplyCount(0);
        insertArticle.setLikeCount(0);
        insertArticle.setState((byte) 0);
        insertArticle.setDeleteState((byte) 0);
        insertArticle.setCreateTime(new Date());
        insertArticle.setUpdateTime(new Date());
        int result = articleMapper.insertSelective(insertArticle);
        if (result <= 0) {
            log.error(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_CREATE));
        }

        //作者文章数+1
        User user = userService.selectById(article.getUserId());
        if (user == null || user.getDeleteState() == 1) {
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        if ( user.getState() == 1) {
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_USER_BANNED));
        }
        userService.addOneArticleCountById(article.getUserId());

        //板块文章数+1
        Board board = boardService.selectById(article.getBoardId());
        if (board == null || board.getDeleteState() == 1) {
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        if (board.getState() == 1) {
            log.info(ResultCode.FAILED_BOARD_BANNED.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_BANNED));
        }
        boardService.addOneArticleCountById(article.getBoardId());
    }

    @Override
    public Article selectDetailById(Long id) {
        if (id==null||id<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectDetailById(id);
        //在sql阶段对deleteState和State进行了校验
        if (article==null){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //浏览数+1
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setVisitCount(article.getVisitCount()+1);
        int result = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
        article.setVisitCount(article.getVisitCount()+1);
        return article;
    }

    @Override
    public Article selectById(Long id) {
        if (id==null||id<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void modify(Article article) {
        if (article==null||article.getId()==null
                ||StringUtil.isEmpty(article.getTitle())
                ||StringUtil.isEmpty(article.getContent())){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setUpdateTime(new Date());
        updateArticle.setTitle(article.getTitle());
        updateArticle.setContent(article.getContent());
        int result = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
    }

    @Override
    public void addOneArticleLikeCountById(Long id) {
        if (id==null||id<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article==null||article.getDeleteState()==1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }if (article.getState()==1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setUpdateTime(new Date());
        updateArticle.setLikeCount(article.getLikeCount()+1);
        int result = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
    }

    @Override
    public void deleteById(Long id) {
        if (id==null||id<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article==null||article.getDeleteState()==1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setDeleteState((byte)1);
        updateArticle.setUpdateTime(new Date());
        int result = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
        boardService.subOneArticleCountById(article.getBoardId());
        userService.subOneArticleCountById(article.getUserId());
    }

    @Override
    public void addOneArticleReplyCountById(Long id) {
        if (id==null||id<=0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if (article==null||article.getDeleteState()==1){
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if (article.getState()==1){
            log.info(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(id);
        updateArticle.setReplyCount(article.getReplyCount()+1);
        int result = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if (result<=0){
            log.warn(ResultCode.FAILED.toString()+", 受影响行数不等于1,");
            throw new ApplicationException(AppResult.fail(ResultCode.FAILED));
        }
    }

}
